﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.IO;
using System.Web.Script.Serialization;
using DataAccessEntLib;
using System.Text;
using DAL;

public partial class Ad_Details : System.Web.UI.Page
{
    BusinessLogic busin = new BusinessLogic();
    BO.Ad bo = new BO.Ad();
    BLL.AdMagnager bll = new BLL.AdMagnager();
    AdDB _objAdDB = new AdDB();
    RegistrationData _objRegistrationData = new RegistrationData();
    protected int ImageLimit = 0, VideoLimit = 0, CurrentUploadImages = 0, AdID = 0;
    string ImageName = string.Empty, folderPath = string.Empty, ImagePath = string.Empty;
    public string Url = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (HttpContext.Current.Request.QueryString["AdID"] != null)
        {
            Session["AdID"] = HttpContext.Current.Request.QueryString["AdID"].ToString();
            if (!IsPostBack)
            {
                BindData();
            }
        }
        else
        {
            Response.Redirect(Common.LogOutPage);
        }
    }


    void BindData()
    {
        if (int.TryParse(Request.QueryString["AdID"], out AdID))
        {
            BindAdList(Convert.ToInt32(HttpContext.Current.Request.QueryString["AdID"].ToString()));
            BindSimilarAds(Convert.ToInt32(HttpContext.Current.Request.QueryString["AdID"].ToString()));
        }
    }


    protected void BindAdList(int AdID)
    {
        if (AdID > 0)
        {
            DataSet ds = new DataSet();
            try
            {
                bo.AdID = AdID;
                ds = _objAdDB.Select_AdMaster(AdID,"AdDetails", 0, true);
                DataRow[] dr1 = ds.Tables[0].Select("AdID=" + AdID.ToString());
                if (dr1.Length > 0)
                {
                    DataRow dr = dr1[0];
                     lblTitle.Text = Convert.ToString(dr["AdTitle"]);
                     lblDesc.Text = Convert.ToString(dr["ProductDescription"]);
                     lnkContactToSeller.HRef = "Cld-Contact-Ad.aspx?AdID="+AdID.ToString();
                     lblSubCategory.Text = Convert.ToString(dr["SubCategory"]);
                     lblItemViews.Text = Convert.ToString(dr["TotalViews"]);
                     lblCategory.Text = Convert.ToString(dr["AdCategory"]);
                     lblPostedDate.Text = Convert.ToString(dr["CreateDate"]);
                     if (!string.IsNullOrEmpty(Convert.ToString(dr["UpdatedDate"])))
                         lblUpdatedDate.Text = Convert.ToString(dr["UpdatedDate"]);
                     else
                         lblUpdatedDate.Text = "&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;";
                     lblState.Text = Convert.ToString(dr["State"]);
                     lblCountry.Text = Convert.ToString(dr["Country"]);
                     lblLandMark.Text = Convert.ToString(dr["LandMark"]); 
                    //ddlCategory.SelectedValue = Convert.ToString(dr["AdCategoryID"]);
                    //ddlSubCategory.SelectedValue = Convert.ToString(dr["SubCategoryID"]);
                }
                ds.Dispose();

                DataSet dsImageVideos = new DataSet();
                bo.AdID = AdID;
                dsImageVideos = bll.Select_AdImageVideos(bo);
                if (!object.Equals(dsImageVideos, null))
                {
                    //if (dsImageVideos.Tables.Count > 0)
                    //{
                    //    if (dsImageVideos.Tables[0].Rows.Count > 0)
                    //    {
                    //        StringBuilder sb = new StringBuilder();
                    //        foreach (DataRow dr in dsImageVideos.Tables[0].Rows)
                    //        {

                    //            sb.Append("<div class='registerion_row registerion_row_none'><div class='registerion_row_left'>Upload Image</div>" +
                    //            "<div class='registerion_row_right'><img src='" + Convert.ToString(dr["Path"]) + "' width='50px' height='50px'/>&nbsp;&nbsp;&nbsp;&nbsp;<input style='width:50px'  type='button' value='Delete' onclick = javascript:__doPostBack('delete','" + Convert.ToString(dr["AdImageVideosID"]) + "')  />" +
                    //            "&nbsp;&nbsp;&nbsp;&nbsp;<a class='bottom_toppup' href='UploadNewImage.aspx?AdImageVideosID=" + Convert.ToString(dr["AdImageVideosID"]) + "&ADID=" + Convert.ToString(dr["AdID"]) + "'>Upload New</a></div></div>");
                    //        }
                    //        //divFiles.InnerHtml = sb.ToString();
                    //    }
                    //}
                    //if (dsImageVideos.Tables.Count > 1)
                    //{
                    //    string[] Content = new string[dsImageVideos.Tables[1].Rows.Count];
                    //    for (int i = 0; i < dsImageVideos.Tables[1].Rows.Count; i++)
                    //    {

                    //        Content[i] = Convert.ToString(dsImageVideos.Tables[1].Rows[i]["Path"]);
                    //    }
                    //   // this.VideoLinks = serializer.Serialize(Content);
                    //}

                }

                SlideImage1.dsimage = dsImageVideos;
            }
            catch (Exception ex)
            {
                //lblMessage.Text = ex.Message;
            }

        }
    }


    protected void BindSimilarAds(int AdID)
    {
        if (AdID > 0)
        {
            DataSet ds = new DataSet();
            try
            {
                bo.AdID = AdID;
                ds = bll.Select_RelatedAds(bo);
                if (!object.Equals(ds, null))
                {
                    if (ds.Tables.Count > 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        int looplengh = 0;
                        if (ds.Tables[0].Rows.Count < 5)
                            looplengh = ds.Tables[0].Rows.Count;
                        else
                            looplengh = 5;

                        for (int i = 0; i < looplengh; i++)
                        {
                            DataRow dr = ds.Tables[0].Rows[i];
                            if (!string.IsNullOrEmpty(Convert.ToString(dr["ImagePath"])))
                            {
                                sb.Append("<li><a href='Cld-Ad-Details.aspx?AdID=" + Convert.ToString(dr["AdID"]) + "'><div class='porduct_img'><img src='" + Convert.ToString(dr["ImagePath"]) + "' border='0'>");
                                sb.Append("</div><p>" + Convert.ToString(dr["AdTitle"]) + "</p></a></li>");
                            }
                            else
                            {
                                sb.Append("<li><a href='Cld-Ad-Details.aspx?AdID=" + Convert.ToString(dr["AdID"]) + "'><div class='porduct_img'><img src='images/no-image.jpg' border='0'>");
                                sb.Append("</div><p>" + Convert.ToString(dr["AdTitle"]) + "</p></a></li>");

                            }

                        }
                        ulRelated.InnerHtml = sb.ToString();
                    }
                }
                ds.Dispose(); 
            }
            catch (Exception ex)
            {
                //lblMessage.Text = ex.Message;
            }

        }
    }
}